package com.example.wanandroid.ui.activity;

import static android.content.ContentValues.TAG;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.DrawableUtils;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.content.DialogInterface;
import android.content.Intent;
import android.content.res.ColorStateList;
import android.graphics.Color;
import android.graphics.drawable.GradientDrawable;
import android.graphics.drawable.StateListDrawable;
import android.os.Bundle;
import android.text.Editable;
import android.util.Log;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;

import com.example.wanandroid.R;
import com.example.wanandroid.app.WanAndroidApp;
import com.example.wanandroid.core.bean.BaseResponse;
import com.example.wanandroid.core.bean.main.collect.FeedArticleListData;
import com.example.wanandroid.core.bean.main.search.TopSearchData;
import com.example.wanandroid.core.bean.project.ProjectClassifyData;
import com.example.wanandroid.core.dao.DaoSession;
import com.example.wanandroid.core.dao.HistoryData;
import com.example.wanandroid.core.dao.HistoryDataDao;
import com.example.wanandroid.core.db.DbHelperImpl;
import com.example.wanandroid.core.http.api.Apis;
import com.example.wanandroid.core.utils.HttpUtil;
import com.example.wanandroid.core.utils.StatusBarUtils;
import com.example.wanandroid.ui.adapter.FlowLayoutAdapter;
import com.example.wanandroid.ui.adapter.HistoryDataListAdapter;
import com.example.wanandroid.ui.view.FlowLayout;

import java.util.ArrayList;
import java.util.List;
import java.util.zip.Inflater;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class SearchActivity extends AppCompatActivity {

    TextView search_tv;
    EditText search_edit;
    ImageButton search_back;
    RecyclerView search_history_rv;
    LinearLayoutManager linearLayoutManager;
    HistoryDataListAdapter historyListAdapter;
    TextView search_history_clear_all_tv;
    DbHelperImpl dbHelper;

    FlowLayout flowLayout;
    FlowLayoutAdapter flowLayoutAdapter;
    List<String> list;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search);
        search_tv = findViewById(R.id.search_tv);
        search_edit = findViewById(R.id.search_edit);
        search_back = findViewById(R.id.search_back_ib);
        search_history_rv = findViewById(R.id.search_history_rv);
        search_history_clear_all_tv = findViewById(R.id.search_history_clear_all_tv);

        flowLayout = findViewById(R.id.top_search_flow_layout);
        initData();


        //数据库操作，获取历史数据操作对象，然后插入搜索历史并获取历史信息，渲染进RecyclerView中展示
        HistoryDataDao historyDataDao = WanAndroidApp.getInstance().getDaoSession().getHistoryDataDao();
        dbHelper = new DbHelperImpl(historyDataDao);
        List<HistoryData> historyData = dbHelper.loadAllHistoryData();

        linearLayoutManager = new LinearLayoutManager(SearchActivity.this);
        historyListAdapter = new HistoryDataListAdapter(SearchActivity.this);
        search_history_rv.setAdapter(historyListAdapter);
        search_history_rv.setLayoutManager(linearLayoutManager);
        historyListAdapter.refreshData(historyData);

        search_back.setOnClickListener(view -> finish());

        search_history_clear_all_tv.setOnClickListener(view -> {
            if(historyData.size()==0){
                showEmptyDialog();
            }else{
                showNormalDialog();
            }
        });

        search_tv.setOnClickListener(view -> {
            String input = search_edit.getText().toString();
            dbHelper.addHistoryData(input);
            recreate();
            Intent intent=new Intent(SearchActivity.this, SearchResultActivity.class);
            intent.putExtra("input",input);
            startActivity(intent);
        });
    }

    private void initData() {
        list = new ArrayList<>();
        Apis apis = HttpUtil.getApi();
        Call<BaseResponse<List<TopSearchData>>> topSearchData = apis.getTopSearchData();
        topSearchData.enqueue(new Callback<BaseResponse<List<TopSearchData>>>() {
            @Override
            public void onResponse(Call<BaseResponse<List<TopSearchData>>> call, Response<BaseResponse<List<TopSearchData>>> response) {
                List<TopSearchData> data = response.body().getData();
                for(TopSearchData data1 : data){
                    list.add(data1.getName());
                }
                flowLayoutAdapter = new FlowLayoutAdapter(SearchActivity.this, list);
                flowLayout.setAdapter(flowLayoutAdapter);
            }

            @Override
            public void onFailure(Call<BaseResponse<List<TopSearchData>>> call, Throwable t) {

            }
        });
    }


    private void showEmptyDialog() {
        final AlertDialog.Builder normalDialog =
                new AlertDialog.Builder(SearchActivity.this);
        normalDialog.setMessage("搜索历史为空！");
        normalDialog.setPositiveButton("确定",
                (dialog, which) -> {

                });
        normalDialog.setNegativeButton("取消",
                (dialog, which) -> {
                    //...To-do
                });
        // 显示
        normalDialog.show();
    }

    private void showNormalDialog() {
        final AlertDialog.Builder normalDialog =
                new AlertDialog.Builder(SearchActivity.this);
        normalDialog.setMessage("是否清空搜索历史?");
        normalDialog.setPositiveButton("确定",
                (dialog, which) -> {
                    dbHelper.clearHistoryData();
                    recreate();
                });
        normalDialog.setNegativeButton("取消",
                (dialog, which) -> {
                    //...To-do
                });
        // 显示
        normalDialog.show();
    }
    @Override
    protected void onResume() {
        super.onResume();
        StatusBarUtils.setStatusBar(this);
    }
}